//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.isunf.common.utils;

import java.io.*;
import java.util.Enumeration;
import org.apache.commons.lang3.StringUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileUtils extends org.apache.commons.io.FileUtils {
    private static Logger a = LoggerFactory.getLogger(FileUtils.class);

    public FileUtils() {
    }

    public static boolean copyFile(String var0, String var1) {
        return copyFileCover(var0, var1, false);
    }

    public static boolean copyFileCover(String var0, String var1, boolean var2) {
        File var3 = new File(var0);
        if (!var3.exists()) {
            a.debug("复制文件失败，源文件 " + var0 + " 不存在!");
            return false;
        } else if (!var3.isFile()) {
            a.debug("复制文件失败，" + var0 + " 不是一个文件!");
            return false;
        } else {
            File var4 = new File(var1);
            if (var4.exists()) {
                if (!var2) {
                    a.debug("复制文件失败，目标文件 " + var1 + " 已存在!");
                    return false;
                }

                a.debug("目标文件已存在，准备删除!");
                if (!delFile(var1)) {
                    a.debug("删除目标文件 " + var1 + " 失败!");
                    return false;
                }
            } else if (!var4.getParentFile().exists()) {
                a.debug("目标文件所在的目录不存在，创建目录!");
                if (!var4.getParentFile().mkdirs()) {
                    a.debug("创建目标文件所在的目录失败!");
                    return false;
                }
            }

            boolean var5 = false;
            FileInputStream var6 = null;
            FileOutputStream var7 = null;

            try {
                var6 = new FileInputStream(var3);
                var7 = new FileOutputStream(var4);
                byte[] var8 = new byte[1024];

                int var23;
                while((var23 = var6.read(var8)) != -1) {
                    var7.write(var8, 0, var23);
                }

                a.debug("复制单个文件 " + var0 + " 到" + var1 + "成功!");
                return true;
            } catch (Exception var21) {
                a.debug("复制文件失败：" + var21.getMessage());
            } finally {
                if (var7 != null) {
                    try {
                        var7.close();
                    } catch (IOException var20) {
                        var20.printStackTrace();
                    }
                }

                if (var6 != null) {
                    try {
                        var6.close();
                    } catch (IOException var19) {
                        var19.printStackTrace();
                    }
                }

            }

            return false;
        }
    }

    public static boolean copyDirectory(String var0, String var1) {
        return copyDirectoryCover(var0, var1, false);
    }

    public static boolean copyDirectoryCover(String var0, String var1, boolean var2) {
        File var3 = new File(var0);
        if (!var3.exists()) {
            a.debug("复制目录失败，源目录 " + var0 + " 不存在!");
            return false;
        } else if (!var3.isDirectory()) {
            a.debug("复制目录失败，" + var0 + " 不是一个目录!");
            return false;
        } else {
            String var4 = var1;
            if (!var1.endsWith(File.separator)) {
                var4 = var1 + File.separator;
            }

            File var5 = new File(var4);
            if (var5.exists()) {
                if (!var2) {
                    a.debug("目标目录复制失败，目标目录 " + var4 + " 已存在!");
                    return false;
                }

                a.debug("目标目录已存在，准备删除!");
                if (!delFile(var4)) {
                    a.debug("删除目录 " + var4 + " 失败!");
                    return false;
                }
            } else {
                a.debug("目标目录不存在，准备创建!");
                if (!var5.mkdirs()) {
                    a.debug("创建目标目录失败!");
                    return false;
                }
            }

            boolean var6 = true;
            File[] var7 = var3.listFiles();

            for(int var8 = 0; var8 < var7.length; ++var8) {
                if (var7[var8].isFile()) {
                    var6 = copyFile(var7[var8].getAbsolutePath(), var1 + var7[var8].getName());
                    if (!var6) {
                        break;
                    }
                }

                if (var7[var8].isDirectory()) {
                    var6 = copyDirectory(var7[var8].getAbsolutePath(), var1 + var7[var8].getName());
                    if (!var6) {
                        break;
                    }
                }
            }

            if (!var6) {
                a.debug("复制目录 " + var0 + " 到 " + var1 + " 失败!");
                return false;
            } else {
                a.debug("复制目录 " + var0 + " 到 " + var1 + " 成功!");
                return true;
            }
        }
    }

    public static boolean delFile(String var0) {
        File var1 = new File(var0);
        if (!var1.exists()) {
            a.debug(var0 + " 文件不存在!");
            return true;
        } else {
            return var1.isFile() ? deleteFile(var0) : deleteDirectory(var0);
        }
    }

    public static boolean deleteFile(String var0) {
        File var1 = new File(var0);
        if (var1.exists() && var1.isFile()) {
            if (var1.delete()) {
                a.debug("删除单个文件 " + var0 + " 成功!");
                return true;
            } else {
                a.debug("删除单个文件 " + var0 + " 失败!");
                return false;
            }
        } else {
            a.debug(var0 + " 文件不存在!");
            return true;
        }
    }

    public static boolean deleteDirectory(String var0) {
        String var1 = var0;
        if (!var0.endsWith(File.separator)) {
            var1 = var0 + File.separator;
        }

        File var2 = new File(var1);
        if (var2.exists() && var2.isDirectory()) {
            boolean var3 = true;
            File[] var4 = var2.listFiles();

            for(int var5 = 0; var5 < var4.length; ++var5) {
                if (var4[var5].isFile()) {
                    var3 = deleteFile(var4[var5].getAbsolutePath());
                    if (!var3) {
                        break;
                    }
                } else if (var4[var5].isDirectory()) {
                    var3 = deleteDirectory(var4[var5].getAbsolutePath());
                    if (!var3) {
                        break;
                    }
                }
            }

            if (!var3) {
                a.debug("删除目录失败!");
                return false;
            } else if (var2.delete()) {
                a.debug("删除目录 " + var0 + " 成功!");
                return true;
            } else {
                a.debug("删除目录 " + var0 + " 失败!");
                return false;
            }
        } else {
            a.debug(var1 + " 目录不存在!");
            return true;
        }
    }

    public static boolean createFile(String var0) {
        File var1 = new File(var0);
        if (var1.exists()) {
            a.debug("文件 " + var0 + " 已存在!");
            return false;
        } else if (var0.endsWith(File.separator)) {
            a.debug(var0 + " 为目录，不能创建目录!");
            return false;
        } else if (!var1.getParentFile().exists() && !var1.getParentFile().mkdirs()) {
            a.debug("创建文件所在的目录失败!");
            return false;
        } else {
            try {
                if (var1.createNewFile()) {
                    a.debug(var0 + " 文件创建成功!");
                    return true;
                } else {
                    a.debug(var0 + " 文件创建失败!");
                    return false;
                }
            } catch (Exception var3) {
                var3.printStackTrace();
                a.debug(var0 + " 文件创建失败!");
                return false;
            }
        }
    }

    public static boolean createDirectory(String var0) {
        String var1 = var0;
        if (!var0.endsWith(File.separator)) {
            var1 = var0 + File.separator;
        }

        File var2 = new File(var1);
        if (var2.exists()) {
            a.debug("目录 " + var1 + " 已存在!");
            return false;
        } else if (var2.mkdirs()) {
            a.debug("目录 " + var1 + " 创建成功!");
            return true;
        } else {
            a.debug("目录 " + var1 + " 创建失败!");
            return false;
        }
    }

    public static void zipFiles(String var0, String var1, String var2) {
        if (var0 == null) {
            a.debug("文件压缩失败，目录 " + var0 + " 不存在!");
        } else {
            File var3 = new File(var0);
            if (var3.exists() && var3.isDirectory()) {
                String var4 = var3.getAbsolutePath();
                File var5 = new File(var2);

                try {
                    ZipOutputStream var6 = new ZipOutputStream(new FileOutputStream(var5));
                    if (!"*".equals(var1) && !"".equals(var1)) {
                        File var7 = new File(var3, var1);
                        if (var7.isFile()) {
                            zipFilesToZipFile(var4, var7, var6);
                        } else {
                            zipDirectoryToZipFile(var4, var7, var6);
                        }
                    } else {
                        zipDirectoryToZipFile(var4, var3, var6);
                    }

                    var6.close();
                    a.debug(var2 + " 文件压缩成功!");
                } catch (Exception var8) {
                    a.debug("文件压缩失败：" + var8.getMessage());
                    var8.printStackTrace();
                }

            } else {
                a.debug("文件压缩失败，目录 " + var0 + " 不存在!");
            }
        }
    }

    public static boolean unZipFiles(String var0, String var1) {
        String var2 = var1;
        if (!var1.endsWith(File.separator)) {
            var2 = var1 + File.separator;
        }

        try {
            ZipFile var3 = new ZipFile(var0);
            ZipEntry var4 = null;
            String var5 = null;
            String var6 = null;
            byte[] var7 = new byte[4096];
            boolean var8 = false;
            Enumeration var9 = var3.getEntries();

            while(true) {
                while(var9.hasMoreElements()) {
                    var4 = (ZipEntry)var9.nextElement();
                    var5 = var4.getName();
                    var6 = var2 + var5;
                    if (var4.isDirectory()) {
                        (new File(var6)).mkdirs();
                    } else {
                        (new File(var6)).getParentFile().mkdirs();
                        File var10 = new File(var6);
                        FileOutputStream var11 = new FileOutputStream(var10);
                        InputStream var12 = var3.getInputStream(var4);

                        int var14;
                        while((var14 = var12.read(var7)) != -1) {
                            var11.write(var7, 0, var14);
                        }

                        var11.close();
                        var12.close();
                    }
                }

                var3.close();
                a.debug("文件解压成功!");
                return true;
            }
        } catch (Exception var13) {
            a.debug("文件解压失败：" + var13.getMessage());
            return false;
        }
    }

    public static void zipDirectoryToZipFile(String var0, File var1, ZipOutputStream var2) {
        if (var1.isDirectory()) {
            File[] var3 = var1.listFiles();
            if (var3.length == 0) {
                ZipEntry var7 = new ZipEntry(a(var0, var1));

                try {
                    var2.putNextEntry(var7);
                    var2.closeEntry();
                } catch (Exception var6) {
                    var6.printStackTrace();
                }

                return;
            }

            for(int var4 = 0; var4 < var3.length; ++var4) {
                if (var3[var4].isFile()) {
                    zipFilesToZipFile(var0, var3[var4], var2);
                } else {
                    zipDirectoryToZipFile(var0, var3[var4], var2);
                }
            }
        }

    }

    public static void zipFilesToZipFile(String var0, File var1, ZipOutputStream var2) {
        FileInputStream var3 = null;
        ZipEntry var4 = null;
        byte[] var5 = new byte[4096];
        boolean var6 = false;
        if (var1.isFile()) {
            try {
                var3 = new FileInputStream(var1);
                var4 = new ZipEntry(a(var0, var1));
                var2.putNextEntry(var4);

                int var9;
                while((var9 = var3.read(var5)) != -1) {
                    var2.write(var5, 0, var9);
                }

                var2.closeEntry();
                var3.close();
                System.out.println("添加文件 " + var1.getAbsolutePath() + " 到zip文件中!");
            } catch (Exception var8) {
                var8.printStackTrace();
            }
        }

    }

    private static String a(String var0, File var1) {
        String var2 = var0;
        if (!var0.endsWith(File.separator)) {
            var2 = var0 + File.separator;
        }

        String var3 = var1.getAbsolutePath();
        if (var1.isDirectory()) {
            var3 = var3 + "/";
        }

        int var4 = var3.indexOf(var2);
        return var3.substring(var4 + var2.length());
    }

    public static String getAbsolutePath(String var0) {
        return "";
    }

    public static void writeFile(String var0, String var1) {
        try {
            if (createFile(var1)) {
                FileWriter var2 = new FileWriter(var1, true);
                BufferedWriter var3 = new BufferedWriter(var2);
                var3.write(var0);
                var3.close();
                var2.close();
            } else {
                a.info("生成失败，文件已存在！");
            }
        } catch (Exception var4) {
            var4.printStackTrace();
        }

    }

    public static void copyFileFromInputStream(InputStream var0, File var1) throws Exception {
        boolean var2 = true;
        long var3 = 0L;
        boolean var5 = false;
        boolean var6 = false;
        FileOutputStream var8 = new FileOutputStream(var1);
        var3 = (long)var0.available();
        int var10 = (int)Math.floor((double)(var3 / 60L));
        int var11 = (int)var3 % 60;
        byte[] var7;
        if (var10 > 0) {
            for(int var9 = 0; var9 < var10; ++var9) {
                var7 = new byte[60];
                var0.read(var7, 0, 60);
                var8.write(var7);
                var8.flush();
            }
        }

        var7 = new byte[var11];
        var0.read(var7, 0, var11);
        var8.write(var7);
        var8.flush();
        var0.close();
        var8.close();
    }
}
